package com.beaven.imclient.fragment;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.beaven.imclient.R;
import com.beaven.imclient.activity.SettingActivity;
import com.beaven.imclient.activity.StartActivity;
import com.beaven.imclient.base.BaseFragment;
import com.beaven.imclient.base.MainApplication;
import com.beaven.imclient.utils.ActivityUtil;
import com.beaven.model.data.helper.AccountHelper;
import com.beaven.model.model.api.RspModel;
import com.beaven.model.model.api.account.AccountRspModel;
import com.beaven.model.model.sp.Account;
import com.raizlabs.android.dbflow.config.FlowManager;
import rx.Subscriber;
import rx.subscriptions.CompositeSubscription;
import timber.log.Timber;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseFragment implements View.OnClickListener {

  public static MineFragment newInstance() {
    return new MineFragment();
  }

  @Override protected int getLayoutId() {
    return R.layout.fragment_mine;
  }

  private TextView text_nick;
  private ProgressDialog dialog;
  private CompositeSubscription subscription;
  private AlertDialog alertDialog;

  @Override protected void initView(View rootView) {
    super.initView(rootView);
    LinearLayout layout_nick = (LinearLayout) rootView.findViewById(R.id.layout_nick);
    LinearLayout layout_change_pwd = (LinearLayout) rootView.findViewById(R.id.layout_change_pwd);
    LinearLayout layout_logout = (LinearLayout) rootView.findViewById(R.id.layout_logout);
    text_nick = (TextView) rootView.findViewById(R.id.text_nick);

    layout_nick.setOnClickListener(this);
    layout_change_pwd.setOnClickListener(this);
    layout_logout.setOnClickListener(this);
  }

  @Override protected void initData() {
    super.initData();
    subscription = new CompositeSubscription();
  }

  @Override public void onResume() {
    super.onResume();
    text_nick.setText(Account.getName());
  }

  @Override public void onPause() {
    super.onPause();
    hideLoading();
    subscription.clear();
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      case R.id.layout_change_pwd:
        SettingActivity.start(getContext(), SettingActivity.TYPE_PWD);
        break;
      case R.id.layout_nick:
        SettingActivity.start(getContext(), SettingActivity.TYPE_NAME);
        break;
      case R.id.layout_logout:
        showConfirm();
        break;
      default:
        break;
    }
  }

  private void showConfirm() {
    alertDialog = ActivityUtil.showDialog(getContext(), getString(R.string.logout), "确认退出",
        new DialogInterface.OnClickListener() {
          @Override public void onClick(DialogInterface dialog, int which) {
            logoutAccount();
          }
        });
    alertDialog.show();
  }

  public void logoutAccount() {
    dialog = ProgressDialog.show(getActivity(), "", getString(R.string.load_now));
    subscription.add(AccountHelper.logout().subscribe(new Subscriber<RspModel<AccountRspModel>>() {
      @Override public void onCompleted() {

      }

      @Override public void onError(Throwable e) {
        hideLoading();
        if (Account.clear(getContext())) {
          FlowManager.destroy();
          StartActivity.start(MainApplication.getInstance());
          android.os.Process.killProcess(android.os.Process.myPid());
          System.exit(0);
        }
      }

      @Override public void onNext(RspModel<AccountRspModel> accountRspModelRspModel) {
        Timber.i(accountRspModelRspModel.getResult().toString());
        hideLoading();
        if (accountRspModelRspModel.isSuccess()) {
          if (Account.clear(getContext())) {
            FlowManager.destroy();
            StartActivity.start(MainApplication.getInstance());
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
          }
        }
      }
    }));
  }

  private void hideLoading() {
    if (dialog != null) {
      dialog.dismiss();
    }
    if (alertDialog != null) {
      alertDialog.dismiss();
    }
  }
}
